/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');
const _service = 'outboundService';

class OutboundController extends base {
  async updateOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);

      const result = await service[_service].updateOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }


  async findOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findOne(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findMany(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findOutboundOrder() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        orderCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const { orderCode, tenantCode } = body;
      const result = await ctx.app.dbs.findOne('SalesOrder', { code: orderCode, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createLot() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        lotCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      console.log('createLot', body);
      const result = await service[_service].createOutboundLot(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findLots() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      console.log('find lots', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findOutboundLots(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteLot() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        lotCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].deleteOutboundLot(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findShippingFeeMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].findShippingFeeMany(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateShippingFee() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
        shippingFee: { type: 'number' },
      };
      console.log('updateShippingFee', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].updateShippingFee(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createShippingFee() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        shippingFee: { type: 'number' },
      };
      console.log('createShippingFee', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].createShippingFee(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteShippingFee() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },

      };
      console.log('deleteShippingFee', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].deleteShippingFee(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async procureReturnOfGoodsOutbound() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      console.log('deleteShippingFee', body);
      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service[_service].procureReturnOfGoodsOutbound(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

}

module.exports = OutboundController;
